<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文档上传</title>
    <link href="/static/bootstrap.min.css" rel="stylesheet">
    <style>
        .result-card {
            margin-bottom: 1rem;
            padding: 1rem;
            border: 1px solid #dee2e6;
            border-radius: 0.25rem;
        }
        .loading {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container mt-5">
        <h1 class="mb-4">文档上传</h1>
        <!-- 文件上传部分 -->
        <div class="card mb-4">
            <div class="card-body">
                <h5 class="card-title">文档上传</h5>
                <form id="uploadForm" class="mb-3" action="/upload/{{ request.args.get('id', '') }}">
                <input type="hidden" name="kb_id" value="{{ request.args.get('id', '') }}">
<input type='hidden' name='id' value='{{ request.args.get('id', '') }}'>
                    <div class="mb-3">
                        <input type="file" class="form-control" id="docxFile" accept=".docx" required>
                <input type="hidden" id="kb_id" name="kb_id" value="{{ request.args.get('id') }}">
                    </div>
                    <div class="form-check mb-3">
                        <input class="form-check-input" type="checkbox" value="1" id="mergeParagraphs" name="mergeParagraphs">
                        <label class="form-check-label" for="mergeParagraphs">
                            智能合并段落
                        </label>
                    </div>
                    <button type="submit" class="btn btn-primary">
                        <span class="spinner-border spinner-border-sm loading" role="status" aria-hidden="true"></span>
                        上传文档
                    </button>
                </form>
                <div id="uploadStatus" class="alert" style="display: none;"></div>

        <!-- 文件列表部分 -->
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">已上传文件</h5>
                <table class="table">
                    <thead>
                        <tr>
                            <th>文件名</th>
                            <th>上传时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for file in uploaded_files %}
                        <tr>
                            <td>{{ file.file_name }}</td>
                            <td>{{ file.upload_time }}</td>
                            <td>
                                <button type="button" class="btn btn-danger btn-sm" onclick="deleteFile('{{ file.id }}')">删除</button>
                                <button type="button" class="btn btn-info btn-sm" onclick="viewFile({{ file.id }})">查看</button>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
            </div>
        </div>
    </div>
    <script src="/static/bootstrap.bundle.min.js"></script>
    <script>
        // 文件上传处理
        document.getElementById('uploadForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const form = e.target;
            const fileInput = form.querySelector('#docxFile');
            const submitBtn = form.querySelector('button[type="submit"]');
            const loading = submitBtn.querySelector('.loading');
            const statusDiv = document.getElementById('uploadStatus');

            if (!fileInput.files[0]) {
                showStatus('请选择文件', 'danger');
                return;
            }

            const formData = new FormData();
                const mergeParagraphs = document.getElementById('mergeParagraphs').checked;
                formData.append('mergeParagraphs', mergeParagraphs ? '1' : '0');
            formData.append('file', fileInput.files[0]);
            const id = document.getElementById('kb_id').value;
         

            try {
                loading.style.display = 'inline-block';
                submitBtn.disabled = true;

                const response = await fetch('/upload/' + id , {
                    method: 'POST',
                    body: formData
                });

                const data = await response.json();
                if (response.ok) {
                    showStatus(data.message, 'success');
                    form.reset();
                } else {
                    showStatus(data.error, 'danger');
                }
            } catch (error) {
                showStatus('上传过程中发生错误', 'danger');
            } finally {
                loading.style.display = 'none';
                submitBtn.disabled = false;
            }
        });

        function showStatus(message, type) {
            const statusDiv = document.getElementById('uploadStatus');
            statusDiv.className = `alert alert-${type}`;
            statusDiv.textContent = message;
            statusDiv.style.display = 'block';
            setTimeout(() => {
                statusDiv.style.display = 'none';
            }, 5000);
        }

        function deleteFile(fileId) {
            fetch(`/deletedoc/${fileId}`, {
                method: 'DELETE'
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    showStatus('文件已删除', 'success');
                    // Optionally, remove the file row from the table
                    // document.querySelector(`tr[data-file-id="${fileId}"]`).remove();
                } else {
                    showStatus(data.error, 'danger');
                }
            })
            .catch(error => {
                showStatus('删除过程中发生错误', 'danger');
            });
        }
    </script>
</body>
</html>